﻿using System;
using System.Linq;
using System.Windows;
using Might.Infrastructure.Annotations.Graphics;
using Might.Infrastructure.ModelicaDOM;
using System.Collections.Generic;

namespace Might.Modelica.Annotations.Graphics {
    ///<summary>
    ///</summary>
    [Mapping("Polygon")]
    public class GraphicsPolygon : GraphicsFilledShape, IGraphicsPolygon {
        ///<summary>
        ///</summary>
        public GraphicsPolygon() {
            points = new List<GraphicsPoint>();
            smooth = Infrastructure.Annotations.Graphics.Smooth.None;
        }

        #region IGraphicsPolygon Member

        public override double Width {
            get {
                double mintop = double.MaxValue;
                double maxtop = double.MinValue;
                foreach (Point point in Points) {
                    if (point.X < mintop) mintop = point.X;
                    if (point.X > maxtop) maxtop = point.X;
                }
                return Math.Max(maxtop - mintop, 1);
            }
            set { }
        }

        public override double Height {
            get {
                double mintop = double.MaxValue;
                double maxtop = double.MinValue;
                foreach (Point point in Points) {
                    if (point.Y < mintop) mintop = point.Y;
                    if (point.Y > maxtop) maxtop = point.Y;
                }
                return Math.Max(maxtop - mintop, 1);
            }
            set { }
        }

        public override double Top {
            get {
                double mintop = double.MaxValue;
                foreach (Point point in Points) {
                    if (point.Y < mintop) mintop = point.Y;
                }
                return mintop - ParentExtent.P1.Y;
            }
            set { }
        }

        public override double Left {
            get {
                double minleft = double.MaxValue;
                foreach (Point point in Points) {
                    if (point.X < minleft) minleft = point.X;
                }
                return minleft - ParentExtent.P1.Y;
            }
            set { }
        }

        public List<Point> Points {
            get { return points.Select(k => k.Point).ToList(); }
            set {
                points.Clear();
                points.AddRange(value.Select(k => new GraphicsPoint(k.X, k.Y)));
            }
        }
        
        public bool Smooth {
            get { return _smooth; }
            set {
                if (_smooth != value) {
                    _smooth = value;
                    OnChanged(this, "Smooth");
                }
            }
        }
        private bool _smooth;

        #endregion

        #region IGraphicsEllipse Members

        ///<summary>
        ///</summary>
        [Mapping("points")]
        public List<GraphicsPoint> points { get; set; }

        ///<summary>
        ///</summary>
        [Mapping("smooth")]
        public Smooth smooth {
            get {
                return Smooth
                    ? Infrastructure.Annotations.Graphics.Smooth.Bezier
                    : Infrastructure.Annotations.Graphics.Smooth.None;
            }
            set { Smooth = (value == Infrastructure.Annotations.Graphics.Smooth.Bezier); }
        }

        public override bool IsDefault {
            get {
                return base.IsDefault &&
                       points.Count == 0 &&
                       smooth == Infrastructure.Annotations.Graphics.Smooth.None;
            }
        }

        #endregion
    }
}